from odoo import fields, models, _
from odoo.exceptions import UserError


class PurchaseInvoiceReturnWizard(models.TransientModel):
    _name = 's.purchsae.invoice.return.wizard'
    _description = 'purchase.invoice.return'

    move_id = fields.Many2one('account.move')
    reason_return = fields.Char('Reason for return', required=True)

    def default_get(self, fields_list):
        result = super().default_get(fields_list)
        active_id = self._context.get('active_id')
        result['move_id'] = active_id
        return result

    def action_confirm(self):
        self.ensure_one()
        if self.move_id.state not in ['posted', 'open']:
            raise UserError(_("You can only return to draft state from 'Open' state."))
        self.move_id.state = 'draft'
        self.move_id.message_post(body=_('invoice return: %s', self.reason_return))
